package com.crr;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.TimeoutException;

public class Productor {
    public static void main(String[] args) {
        //1创建连接工厂 创建连接
        ConnectionFactory factory = new ConnectionFactory();
        factory.setVirtualHost("/");
        factory.setPort(5672);
        factory.setUsername("user");
        factory.setPassword("chen1116.");
        // factory.setHost("127.0.0.1");

        //2创建连接
        try (Connection conn = factory.newConnection();
             //3创建信道
             Channel channel = conn.createChannel()) {
            //4 启用发布确认
            channel.confirmSelect();
            //存储消息
            ConcurrentNavigableMap msgMap = new ConcurrentSkipListMap();
            //接收服务器确认的消息
            channel.addConfirmListener((deliveryTag, multiple) -> {
                System.out.println("服务器确认消息：" + deliveryTag);
            }, (deliveryTag, multiple) -> {
                System.out.println("服务器没有收到消息：" + deliveryTag);
            });
            //声明队列

            String queueName = "msg_ttl";
            //参数设置队列ttl
           /* Map<String,Object> arg=new HashMap<>
                    ();
            arg.put("x-message-ttl",5000);*/
            //队列参数设置过期
            Map<String, Object> arg = new
                    HashMap<String, Object>();
            arg.put("x-expires", 1800000);
            channel.queueDeclare(queueName, false, false, false, arg);
            //发送消息
            //队列消息设置ttl
           /* AMQP.BasicProperties properties=new AMQP.BasicProperties()
                    .builder()
                    .expiration("5000")//消息过期时间
                            .build();*/

            channel.basicPublish("", queueName, null, "消息过期".getBytes());

        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (TimeoutException e) {
            throw new RuntimeException(e);
        }

    }

}
